package com.example.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.google.common.collect.Lists;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.ApiKey;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Swagger2自动配置
 * 
 * @author xie.m
 * @version 0.1.0
 *
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

	@Bean
	public Docket createRestApi() {
		return new Docket(DocumentationType.SWAGGER_2).securitySchemes(Lists.newArrayList(apiKey())).apiInfo(apiInfo())
				.select().apis(RequestHandlerSelectors.basePackage("com.example.demo")).paths(PathSelectors.any())
				.build();
	}

	private ApiInfo apiInfo() {
		return new ApiInfoBuilder().title("My App").version("1.0.0").build();
	}

	/**
	 * 添加全局参数
	 * 
	 * @return
	 */
	private ApiKey apiKey() {
		return new ApiKey("apikey", "Authorization", "header");
	}

}